package com.qf.file.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图片的工具类
 *
 * @author xuzhong
 */
public class ImageUtil {

    /**
     * 放缩图片
     */
    public static BufferedImage zoom(BufferedImage sourceImage,
                                     int width, int height) {
        //设置放缩尺寸
        Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        //申请一个空白的图片，但是"像素点格式"和原图片一样
        BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
        //通常情况下默认的像素点格式： BufferedImage.TYPE_INT_ARGB
        //一个图片由多个像素点构成，每一个像素点又应该是RGB配比构成，如果想要支持透明
        //还应该描述透明度，透明度用A表示。
        //因此，INT_ARGB 指的是，用一个int类型来描述一个像素点，
        //也就是Color这个类的策略
        Graphics graphics = zoomImage.getGraphics();
        //绘制图片
        graphics.drawImage(image, 0, 0, null);
        return zoomImage;
    }

    public static BufferedImage italicMark(BufferedImage sourceImage, String mark) {
        Graphics2D graphics = (Graphics2D) sourceImage.getGraphics();
        graphics.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 30));
        graphics.setColor(new Color(255, 255, 255, 116));
        int width = sourceImage.getWidth();
        int height = sourceImage.getHeight();
        int markWidth = graphics.getFontMetrics().stringWidth(mark);
        int markHeight = graphics.getFontMetrics().getHeight();
        //旋转参数是弧度制
        graphics.rotate(Math.PI / 6);
        int rowNumber=0;
        for (int col = -height; col < height; col += markHeight * 1.5,rowNumber++) {
            for (int row = -rowNumber*markWidth; row < width * 2; row += markWidth * 1.5) {
                graphics.drawString(mark, row, col);
            }
        }
        return sourceImage;
    }


    public static BufferedImage waterMark(BufferedImage sourceImage, String mark) {
        //graphics是画布对象。
        Graphics graphics = sourceImage.getGraphics();
        graphics.setFont(new Font("宋体", Font.BOLD, 20));

        //我们可以在画布上绘制任何东西。
        int width = sourceImage.getWidth();
        int height = sourceImage.getHeight();
        int markWidth = graphics.getFontMetrics().stringWidth(mark);
        int markHeight = (int) (graphics.getFontMetrics().getHeight() * 0.22);
        int x = width - markWidth;
        int y = height - markHeight;
        graphics.drawString(mark, x, y);
        return sourceImage;
    }


    public static void main(String[] args) throws IOException {
//        BufferedImage image = ImageIO.read(new File("C:\\JAVA2203\\fengjing.jpg"));
//        BufferedImage result = zoom(image, 768, 432);
//        ImageIO.write(result,"jpg",new File("C:\\JAVA2203\\fengjing_768x432.jpg"));

//        BufferedImage image=ImageIO.read(new File("C:\\JAVA2203\\fengjing_768x432.jpg"));
//        BufferedImage result = waterMark(image, "@千锋互联");
//        ImageIO.write(result,"jpg",new File("C:\\JAVA2203\\fengjing_mark.jpg"));


        BufferedImage image = ImageIO.read(new File("C:\\JAVA2203\\fengjing_768x432.jpg"));
        BufferedImage result = italicMark(image, "仅账号认证使用");
        ImageIO.write(result, "jpg", new File("C:\\JAVA2203\\fengjing_italic.jpg"));

    }
}
